#include "controlmode.h"
#include "ui_controlmode.h"

ControlMode::ControlMode(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ControlMode)
{
    ui->setupUi(this);
    this->show();
    thread = new QThread(this);
    load = new Loading(this);
    load->moveToThread(thread);
    connect(this,&ControlMode::Lclose,load,&Loading::closeLoading);
    thread->start();
    load->show();
    pushButtonStyle();
    QBluetoothDeviceDiscoveryAgent *discoveryAgent;
    discoveryAgent = new QBluetoothDeviceDiscoveryAgent;
    connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(discoverBlueTooth(QBluetoothDeviceInfo)));
    connect(discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished()));
    discoveryAgent->start();
}

void ControlMode::discoveryFinished()
{
    qDebug()<<"discoveryFinished";

}

void ControlMode::discoverBlueTooth(QBluetoothDeviceInfo info)
{
    QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name());
    if(info.name()=="HC-04")
    {
        BTaddress = info.address().toString();
        static QString serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
        socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
        socket->connectToService(QBluetoothAddress(BTaddress), QBluetoothUuid(serviceUuid),QIODevice::ReadWrite);
        connect(socket,SIGNAL(readyRead()), this, SLOT(readBluetoothDataEvent()));
        connect(socket,SIGNAL(connected()), this, SLOT(bluetoothConnectedEvent()));
    }
    qDebug()<<label;
}


void ControlMode::readBluetoothDataEvent()
{
   char data[100];
   qint64 len = socket->read((char *)data, 100);

   QByteArray qa2((char*)data,len);
   //QString qstr(qa2.toHex());//
   QString qstr2(qa2);//
   QString a1 = qstr2.mid(0,1);
   QString a2 = qstr2.mid(1);
   /*如果接收到超声波模块测距信号，则显示距离*/
   if(a1 == DistanceSignal)
   {
       ui->label_ShowDistanceValue->setText(a2+"cm");
   }
}

void ControlMode::bluetoothConnectedEvent()
{
    qDebug()<<"bluetoothConnectedEvent";
    emit Lclose();
}

void ControlMode::bluetoothDataSend(QString str)
{
    QByteArray arrayData;
    arrayData = str.toUtf8();
    socket->write(arrayData);
}

void ControlMode::pushButtonStyle()
{
    ui->ForwardPushButton->setStyleSheet("QPushButton{"
                                         "border-image:url(:/photo/blankforward.png);""}"
                                         "QPushButton:hover{"
                                         "border-image:url(:/photo/forward.png);"
                                         "}"
                                         );
    ui->LeftPushButton->setStyleSheet("QPushButton{"
                                      "border-image:url(:/photo/blankleft.png);""}"
                                      "QPushButton:hover{"
                                      "border-image:url(:/photo/left.png);"
                                      "}"
                                      );
    ui->RightPushButton->setStyleSheet("QPushButton{"
                                       "border-image:url(:/photo/blankright.png);""}"
                                       "QPushButton:hover{"
                                       "border-image:url(:/photo/right.png);"
                                       "}"
                                       );
    ui->BackPushButton->setStyleSheet("QPushButton{"
                                      "border-image:url(:/photo/blankback.png);""}"
                                      "QPushButton:hover{"
                                      "border-image:url(:/photo/back.png);"
                                      "}"
                                      );
    ui->StopPushButton->setStyleSheet("QPushButton{"
                                      "border-image:url(:/photo/stop.png);""}"
                                      "QPushButton:hover{"
                                      "border-image:url(:/photo/fullstop.png);"
                                      "}"
                                      );
    ui->ledLabel->setStyleSheet("min-width: 120; min-height: 120;max-width:120x; max-height: 120x;border-radius: 60;  border:1px solid black;"
                                "background-color: rgb(0, 255, 255);"
                                "border-image: url(:/photo/led.png);"
                                );
}

void ControlMode::showLedColour()
{
    QString ledColor = "background-color: rgb("+QString::number(Led_R)+","+QString::number(Led_G)+","+QString::number(Led_B)+");";
    ui->ledLabel->setStyleSheet("min-width: 120; min-height: 120;max-width:120x; max-height: 120x;border-radius: 60;  border:1px solid black;"+ledColor+"border-image: url(:/photo/led.png);");
}

ControlMode::~ControlMode()
{
    thread->quit();
    thread->wait();
    delete ui;
}



void ControlMode::on_ForwardPushButton_pressed()
{
    bluetoothDataSend(ForwardSignal);
}

void ControlMode::on_RightPushButton_pressed()
{
    bluetoothDataSend(RightSignal);
}

void ControlMode::on_LeftPushButton_pressed()
{
    bluetoothDataSend(LeftSignal);
}

void ControlMode::on_BackPushButton_pressed()
{
    bluetoothDataSend(BackSignal);
}

void ControlMode::on_StopPushButton_pressed()
{
    bluetoothDataSend(StopSignal);
}

void ControlMode::on_ForwardPushButton_released()
{
    bluetoothDataSend(StopSignal);
}

void ControlMode::on_LeftPushButton_released()
{
    bluetoothDataSend(StopSignal);
}

void ControlMode::on_RightPushButton_released()
{
    bluetoothDataSend(StopSignal);
}

void ControlMode::on_BackPushButton_released()
{
    bluetoothDataSend(StopSignal);
}

void ControlMode::on_horizontalSlider_Led_B_sliderMoved(int position)
{
    Led_B = position  * 2.55;
    showLedColour();
    QString ledcolor = QString("%1%2%3").arg(Led_R,3,10,QLatin1Char('0')).arg(Led_G,3,10,QLatin1Char('0')) .arg(Led_B,3,10,QLatin1Char('0'));    //临时变量，存储发送led颜色数据
    bluetoothDataSend(LedSignal+ledcolor);
    }

void ControlMode::on_horizontalSlider_Led_G_sliderMoved(int position)
{
    Led_G = position  * 2.55;
    showLedColour();
    QString ledcolor = QString("%1%2%3").arg(Led_R,3,10,QLatin1Char('0')).arg(Led_G,3,10,QLatin1Char('0')) .arg(Led_B,3,10,QLatin1Char('0'));    //临时变量，存储发送led颜色数据
    bluetoothDataSend(LedSignal+ledcolor);
}

void ControlMode::on_horizontalSlider_Led_R_sliderMoved(int position)
{
    Led_R = position  * 2.55;
    showLedColour();
    QString ledcolor = QString("%1%2%3").arg(Led_R,3,10,QLatin1Char('0')).arg(Led_G,3,10,QLatin1Char('0')) .arg(Led_B,3,10,QLatin1Char('0'));    //临时变量，存储发送led颜色数据
    bluetoothDataSend(LedSignal+ledcolor);
}

void ControlMode::on_pushButton_LedSwitch_clicked()
{
    if(ui->pushButton_LedSwitch->text() == "ON")        /*灯的开关由关转开*/
    {
         ui->pushButton_LedSwitch->setText("OFF");
         ui->horizontalSlider_Led_B->setValue(255);
         ui->horizontalSlider_Led_G->setValue(255);
         ui->horizontalSlider_Led_R->setValue(255);
         Led_B = 255;
         Led_G = 255;
         Led_R = 255;
         showLedColour();
         bluetoothDataSend(LedOnSignal);
    }
    else if (ui->pushButton_LedSwitch->text() == "OFF") {
        ui->pushButton_LedSwitch->setText("ON");
        ui->horizontalSlider_Led_B->setValue(0);
        ui->horizontalSlider_Led_G->setValue(0);
        ui->horizontalSlider_Led_R->setValue(0);
        Led_B = 0;
        Led_G = 0;
        Led_R = 0;
        showLedColour();
        bluetoothDataSend(LedOffSignal);
    }
}

void ControlMode::on_pushButton_SetBlue_clicked()
{
    ui->horizontalSlider_Led_B->setValue(255);
    ui->horizontalSlider_Led_G->setValue(0);
    ui->horizontalSlider_Led_R->setValue(0);
    Led_B = 255;
    Led_G = 0;
    Led_R = 0;
    showLedColour();
    bluetoothDataSend(LedBlueSignal);
}

void ControlMode::on_pushButton_SetGreen_clicked()
{
    ui->horizontalSlider_Led_B->setValue(0);
    ui->horizontalSlider_Led_G->setValue(255);
    ui->horizontalSlider_Led_R->setValue(0);
    Led_B = 0;
    Led_G = 255;
    Led_R = 0;
    showLedColour();
    bluetoothDataSend(LedGreenSignal);
}

void ControlMode::on_pushButton_SetRed_clicked()
{
    ui->horizontalSlider_Led_B->setValue(0);
    ui->horizontalSlider_Led_G->setValue(0);
    ui->horizontalSlider_Led_R->setValue(255);
    Led_B = 0;
    Led_G = 0;
    Led_R = 255;
    showLedColour();
    bluetoothDataSend(LedRedSignal);
}

void ControlMode::on_horizontalSlider_Servo_sliderPressed()
{
    int Temp;       //存储舵机转向的角度（0-180）,滑条的范围是0到100，所以得乘上1.8
    Temp = ui->horizontalSlider_Servo->value() * 1.8;
    bluetoothDataSend(ServoSignal+QString::number(Temp));
}

void ControlMode::on_horizontalSlider_Servo_sliderReleased()
{
    int Temp;       //存储舵机转向的角度（0-180）,滑条的范围是0到100，所以得乘上1.8
    Temp = ui->horizontalSlider_Servo->value() * 1.8;
    bluetoothDataSend(ServoSignal+QString::number(Temp));
}

void ControlMode::on_pushButton_ServoLeft_clicked()
{
    bluetoothDataSend(ServoSignal+QString::number(0));
}

void ControlMode::on_pushButton_ServoCenter_clicked()
{
    bluetoothDataSend(ServoSignal+QString::number(90));
}

void ControlMode::on_pushButton_ServoRight_clicked()
{
    bluetoothDataSend(ServoSignal+QString::number(180));
}


void ControlMode::on_verticalSlider_speed_sliderReleased()
{
    int speed;
    speed = ui->verticalSlider_speed->value();
    ui->label_SpeedValue->setText(QString::number(speed)+"%");
    bluetoothDataSend(SpeedSignal+QString::number(speed));
}
